package com.common.client;

import java.util.ArrayList;
import java.util.Collection;

import com.common.shared.SharedClass;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.VLayout;

public class GridPanel extends VLayout {

	private ToolbarAndMenu mt;

	public GridPanel(String name, SharedClass sharedClass,
			@SuppressWarnings("rawtypes") Collection sharedClasses,
			ArrayList<ToolbarAndMenuItems> toolbarAndMenuItems) {
		super();
		mt = new ToolbarAndMenu(toolbarAndMenuItems);
		mt.getToolStrip().setWidth100();
		mt.getToolStrip().setHeight("5%");
		this.addMember(mt.getToolStrip());
		ListGrid listGrid = new ListGrid();
		listGrid.setWidth100();
		listGrid.setHeight("85%");
		listGrid.setAutoFetchData(true);
		listGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
			@Override
			public void onSelectionChanged(SelectionEvent event) {
				GridPanel.this.mt.setItemEnable(
						ToolbarAndMenuItems.ACTION_DELETE,
						event.getRecord() != null);
				GridPanel.this.mt.setItemEnable(
						ToolbarAndMenuItems.ACTION_EDIT,
						event.getRecord() != null);
			}
		});
		createDatasource(name, sharedClass, sharedClasses, listGrid);
		listGrid.setContextMenu(mt.getMenu());
		addMember(listGrid);

	}

	private void createDatasource(String id, SharedClass obj,
			@SuppressWarnings("rawtypes") Collection sharedClasses,
			ListGrid listGrid) {

		// DSFieldOptions[] fieldOptions = obj.getFieldOptions();
		// DataSourceField[] dsFields = new
		// DataSourceField[fieldOptions.length];
		// ListGridField[] lgFields = new ListGridField[fieldOptions.length];
		// for (int i = 0; i < fieldOptions.length; i++) {
		// DSFieldOptions dsFieldOptions = fieldOptions[i];
		// FieldType type = null;
		// switch (dsFieldOptions.getFieldType()) {
		// case DSFieldOptions.FT_STRING:
		// type = FieldType.TEXT;
		// break;
		// case DSFieldOptions.FT_BOOLEAN:
		// type = FieldType.BOOLEAN;
		// break;
		// case DSFieldOptions.FT_DOUBLE:
		// type = FieldType.FLOAT;
		// break;
		// case DSFieldOptions.FT_LONG:
		// case DSFieldOptions.FT_INTEGER:
		// type = FieldType.INTEGER;
		// break;
		// default:
		// type = FieldType.TEXT;
		// }
		// dsFields[i] = new DataSourceField(dsFieldOptions.getFieldName(),
		// type, dsFieldOptions.getFieldTitle());
		// lgFields[i] = new ListGridField(dsFieldOptions.getFieldName(),
		// dsFieldOptions.getFieldTitle());
		// lgFields[i].setHidden(!dsFieldOptions.isVisible());
		// lgFields[i].setWidth(dsFieldOptions.getFieldLength());
		//
		// }
		// DataSource ds = new DataSource();
		// ds.setID(id);
		// ds.setFields(dsFields);
		//
		// ds.setClientOnly(true);
		// ArrayList<ListGridRecord> records = new ArrayList<ListGridRecord>();
		//
		// for (Object obj1 : sharedClasses) {
		// ListGridRecord r = new ListGridRecord();
		// for (int i = 0; i < fieldOptions.length; i++) {
		// String fn = fieldOptions[i].getFieldName();
		// r.setAttribute(fn, ((SharedClass) obj1).get_Value(fn));
		// }
		// records.add(r);
		// }
		// ds.setTestData(records.toArray(new ListGridRecord[] {}));
		// listGrid.setFields(lgFields);
		// listGrid.setDataSource(ds);

	}
}
